package com.macbulldev.poc.testgin.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.transaction.annotation.Transactional;


@javax.persistence.Entity
@Configurable
public class AppRole implements GrantedAuthority
{

	@NotNull
	@Column(nullable=false)
    private String roleString;

    @javax.persistence.ManyToOne( fetch=FetchType.EAGER)
    @javax.persistence.JoinColumn(name = "email")
    private com.macbulldev.poc.testgin.domain.AppUser appUser;

	public AppRole(AppUser user, String role) {
		this.appUser=user;
		roleString=role;
		
	}

	public AppRole() {
	}

	@Override
	public String getAuthority() {
		
		return this.roleString;
	}

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            AppRole attached = this.entityManager.find(AppRole.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        AppRole merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new AppRole().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countAppRoles() {    
        return (Long) entityManager().createQuery("select count(o) from AppRole o").getSingleResult();        
    }

	public static List<AppRole> findAllAppRoles() {    
        return entityManager().createQuery("select o from AppRole o").getResultList();        
    }

	public static AppRole findAppRole(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of AppRole");        
        return entityManager().find(AppRole.class, id);        
    }

	public static List<AppRole> findAppRoleEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from AppRole o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public static List<AppRole> findUnassociatedRoleByRoleString(String role) {

		String hql="select o from AppRole o where o.appUser is null and o.roleString = ?";
		Query q = entityManager().createQuery(hql);
		q.setParameter(1, role);
		
		return q.getResultList();        

	}
	public static List<AppRole> findAllUnassociatedRoles() {
		String hql="select o from AppRole o where o.appUser is null and o.roleString is not null";
		Query q = entityManager().createQuery(hql);		
		return q.getResultList();     
	}
	
	@Transactional 
	public static Integer deleteRolesByEmail(String email) {
	    int x =-1;


	    
		String hql="delete from AppRole o where o.appUser.email = ?";
		Query q = entityManager().createQuery(hql);		
		q.setParameter(1, email);
		x= q.executeUpdate();   

		return x;

	}

	
	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Authority: ").append(getAuthority()).append(", ");        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("RoleString: ").append(getRoleString()).append(", ");        
        sb.append("AppUser: ").append(getAppUser());        
        return sb.toString();        
    }

	public String getRoleString() {    
        return this.roleString;        
    }

	public void setRoleString(String roleString) {    
        this.roleString = roleString;        
    }

	public AppUser getAppUser() {    
        return this.appUser;        
    }

	public void setAppUser(AppUser appUser) {    
        this.appUser = appUser;        
    }

	
	


}
